# 通信终端管理后台
from django.shortcuts import render
from com.user.sign_in_controller import is_sign_in
from django.shortcuts import HttpResponseRedirect
from smart_monitor.models import Communicator as c_db_m
from my_models.communicator_model import Communicator as c_m


class Communicator(object):
    def __init__(self, request):
        self.request = request

    def run(self):
        sign_in, user_name = is_sign_in(self.request)
        if not sign_in:
            return HttpResponseRedirect("/user/sign_in")

        communicates_set = c_db_m.objects.filter(user_name=self.request.session["user_id"])
        communicates = []
        for communicate_obj in communicates_set:
            communicates.append(c_m(communicate_obj.id,
                                    communicate_obj.communicator_id,
                                    communicate_obj.communicator_info,
                                    communicate_obj.communicator_state,
                                    user_name))
        data_set = {
            "user_name": user_name,
            "communicators": communicates
        }
        return render(self.request, "communicator_console.html", data_set)
